﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using pttk_htt_hr.Constant;
using pttk_htt_hr.DTO;

namespace pttk_htt_hr.DAO
{
    class PhieuLapThucDonTuanToiDAO
    {
        #region Select
        /// <summary>
        /// Lay_PhieuLapThucDonTuanToi
        /// </summary>
        /// <returns>PhieuLapThucDonTuanToiDTO</returns>
        public static PhieuLapThucDonTuanToiDTO Lay_PhieuLapThucDonTuanToi(List<SqlParameter> parameters)
        {
            DBMS dbms = new DBMS();
            dbms.Connect();

            PhieuLapThucDonTuanToiDTO pltdtt = new PhieuLapThucDonTuanToiDTO();

            DataTable dt = dbms.ExcuteProcedure_Select_DataTable(StoreProcedureConstant.SP_SELECT_PHIEU_LAP_TD_TUAN_TOI_ById, parameters.ToArray());
           
            DataRow dr = dt.Rows[0];

            pltdtt.MaPLTDTT = dr[TableColumnMapping.MaPLTDTT].ToString();
            pltdtt.NgayLap = dr[TableColumnMapping.NGAYLAP].ToString();
            pltdtt.GhiChu = dr[TableColumnMapping.GHICHU].ToString();

            dbms.Disconnect();

            return pltdtt;
        }

        public static List<Dictionary<string,string>> Get_List(List<SqlParameter> parameters, bool hasConds)
        {
            DBMS dbms = new DBMS();
            dbms.Connect();

            List<Dictionary<string, string>> LstDic = new List<Dictionary<string, string>>();

            DataTable dt;
            if (hasConds)
            {
                dt = dbms.ExcuteProcedure_Select_DataTable(StoreProcedureConstant.SP_SELECT_PHIEU_LAP_TD_TUAN_TOI_ByConds, parameters.ToArray());
            }
            else
            {
                dt = dbms.ExcuteProcedure_Select_DataTable(StoreProcedureConstant.SP_SELECT_PHIEU_LAP_TD_TUAN_TOI, parameters.ToArray());
            }

            PhieuLapThucDonTuanToiDTO pltdtt;
            Dictionary<string, string> row;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                row = new Dictionary<string, string>();
                pltdtt = new PhieuLapThucDonTuanToiDTO();

                row.Add(TableColumnMapping.MaPLTDTT, dr[TableColumnMapping.MaPLTDTT].ToString());
                row.Add(TableColumnMapping.NGAYLAP,dr[TableColumnMapping.NGAYLAP].ToString());
                row.Add(TableColumnMapping.GHICHU,dr[TableColumnMapping.GHICHU].ToString());

                // Add to list
                LstDic.Add(row);
            }
           
            dbms.Disconnect();

            return LstDic;

        }
        #endregion


        #region Update
        public static int CapNhat_PhieuLapThucDonToi(List<SqlParameter> parameters)
        {
            DBMS dbms = new DBMS();
            dbms.Connect();

            int result = dbms.ExcuteProcedure_InsertUpdateDelete_Int(StoreProcedureConstant.SP_UPDATE_PHIEU_LAP_TD_TUAN_TOI, parameters.ToArray());

            dbms.Disconnect();

            return result;
        }
        #endregion


        #region Delete
        
        /// <summary>
        /// Delete_PhieuLapThucDonToi
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>

        public static int Delete_PhieuLapThucDonToi(List<SqlParameter> parameters)
        {
            DBMS dbms = new DBMS();
            dbms.Connect();

            int result = dbms.ExcuteProcedure_InsertUpdateDelete_Int(StoreProcedureConstant.SP_DELETE_PHIEU_LAP_TD_TUAN_TOI, parameters.ToArray());

            dbms.Disconnect();

            return result;
        }

        public static int DeleteAll_PhieuLapThucDonToi(List<List<SqlParameter>> parameters)
        {
            int res = 0;
            DBMS dbms = new DBMS();
            dbms.Connect();

            foreach (var item in parameters)
            {
                res = dbms.ExcuteProcedure_InsertUpdateDelete_Int(StoreProcedureConstant.SP_DELETE_PHIEU_LAP_TD_TUAN_TOI, item.ToArray());
            }

            dbms.Disconnect();

            
            return res;
        }

        #endregion


        #region Insert

        /// <summary>
        /// Them_PhieuLapThucDonToi
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns>int</returns>
        public static int Them_PhieuLapThucDonToi(List<SqlParameter> parameters)
        {
            DBMS dbms = new DBMS();
            dbms.Connect();

            int result = dbms.ExcuteProcedure_InsertUpdateDelete_Int(StoreProcedureConstant.SP_INSERT_PHIEU_LAP_TD_TUAN_TOI, parameters.ToArray());

            dbms.Disconnect();

            return result;
        }
        #endregion
        
    }
}
